//
//  MyInstitutionViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/29.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "MyInstitutionViewController.h"
#import "MyInstituCell.h"
#import "OrganationApplayINViewController.h"
#import "MyInstituModel.h"
#import "MyInstituViewUpateController.h"
@interface MyInstitutionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
/**
 编辑
 */
@property (nonatomic, strong) UIButton *rightBtn;

/**
 删除
 */
@property (nonatomic, strong) UIButton *deleteBtn;

/**
 全选
 */
@property (nonatomic, strong) UIButton *allBtn;

@property (nonatomic, strong) NSMutableArray *selectArray;

@property(nonatomic,strong)UIView *bottomView;
@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation MyInstitutionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的机构";
    [self getAllInstituDataList];
    [self createNavagationBackBtn];
    [self createTabelveiew];
    // Do any additional setup after loading the view.
}
-(void)createNavagationBackBtn{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 6, 67, 23);
    UIImageView *backImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"navigator_btn_back"]];
    backImg.frame = CGRectMake(-10, 0, 22, 22);
    [backBtn addSubview:backImg];
    UILabel *backText = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 65, 22)];
//    backText.text = @"返回";
    backText.font = [UIFont systemFontOfSize:17];
    [backText setBackgroundColor:[UIColor clearColor]];
    [backText setTextColor:[UIColor whiteColor]];
    [backBtn addSubview:backText];
    [backBtn addTarget:self action:@selector(leftBarButtonItemPressed:) forControlEvents:UIControlEventTouchUpInside];
    [backImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backBtn.mas_left).offset(-10);
        make.centerY.equalTo(backBtn.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    [backText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(backImg.mas_right).offset(2);
        make.centerY.equalTo(backBtn.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(65, 22));
    }];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    [self.navigationItem setLeftBarButtonItem:leftButton];
}
-(void)leftBarButtonItemPressed:(UIButton *)btn{
    NSArray *viewcontrollers = self.navigationController.viewControllers;
    if (viewcontrollers.count > 1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count - 1] == self) {
            //push方式
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    else{
        //present方式
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
-(void)createTabelveiew{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64 - 49) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerClass:[MyInstituCell class] forCellReuseIdentifier:@"MyInstituCell"];
    self.tableView.tableFooterView = [[UIView alloc] init];
      [self setupChildViews];
    self.tableView.estimatedRowHeight = 200;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MyInstituCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"MyInstituCell"];
    for (UIView *subVies in cell.contentView.subviews) {
        [subVies removeFromSuperview];
    }
    cell.model = self.dataArray[indexPath.row];
    return cell;
}
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return scaleTopAndBottom(12) * 2 + 72;
//}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyInstituModel *model = self.dataArray[indexPath.row];
    MyInstituViewUpateController *upVc = [[MyInstituViewUpateController alloc] init];
    upVc.needUpdateInsituId = model.Id;
    [self.navigationController pushViewController:upVc animated:YES];
}
- (void)setupChildViews {
    UIBarButtonItem *spatce = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
    UIBarButtonItem *rightBaritem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBtn];
    spatce.width = -10;
    self.navigationItem.rightBarButtonItems = @[spatce,rightBaritem];
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT - 49, WIDTH, 49)];
    [self.view addSubview:_bottomView];
    [self setUIAbovewView:_bottomView];
    [self.view bringSubviewToFront:_bottomView];
    //    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.deleteBtn];
    
}
-(void)setUIAbovewView:(UIView *)view{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = COLOR_HEX(0xF9D025);
    [button setTitle:@"+申请机构入驻" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, WIDTH, 49);
    [button addTarget:self action:@selector(goOrganationPage) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];
    
}
-(void)goOrganationPage{
    OrganationApplayINViewController *applyIn = [[OrganationApplayINViewController alloc] init];
    [self.navigationController pushViewController:applyIn animated:YES];
}
- (UIButton *)rightBtn {
    if (!_rightBtn) {
        _rightBtn = [[UIButton alloc] init];
        [_rightBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [_rightBtn setTitle:@"完成" forState:UIControlStateSelected];
        
        [_rightBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [_rightBtn addTarget:self action:@selector(rightBtnAction) forControlEvents:UIControlEventTouchUpInside];
        [_rightBtn sizeToFit];
        _rightBtn.hidden = YES;
    }
    return _rightBtn;
}
-(void)getAllInstituDataList{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *sessionId = [[NSUserDefaults standardUserDefaults] objectForKey:@"sessionId"];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:sessionId,@"sessionId", nil];
    NSString *url = ServerIp@"/institutionApp/myInstitutions";
    [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *listArra = jsonDic[@"list"];
        for (NSDictionary *dic in listArra) {
            MyInstituModel *model = [MyInstituModel modelWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
        NSLog(@"%@",jsonDic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
-(void)rightBtnAction{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
